package com.oazon.moco.lan.core.http;


import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion;

import java.net.InetAddress;
import java.util.List;

import android.util.Log;

public class HttpJsonRequestEncoder extends AbsHttpJsonEncoder<HttpJsonRequest>{

	@Override
	protected void encode(ChannelHandlerContext ctx, HttpJsonRequest message,
			List<Object> out) throws Exception {
		
		Log.d("Encode", "HttpJsonRequestEncoder");
		
		ByteBuf body = encode(ctx, message.getBody());
		FullHttpRequest request = message.getRequest();
		
		if(request == null){
			request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, 
					HttpMethod.GET, "/do",body);
			HttpHeaders headers = request.headers();
			headers.set(HttpHeaders.Names.HOST,InetAddress.getLocalHost().getHostAddress());
			headers.set(HttpHeaders.Names.CONNECTION,HttpHeaders.Values.KEEP_ALIVE);
			headers.set(HttpHeaders.Names.ACCEPT_ENCODING,HttpHeaders.Values.GZIP.toString()+","+HttpHeaders.Values.DEFLATE.toString());
		}
		HttpHeaders.setContentLength(request, body.readableBytes());
		out.add(request);
	}

}
